import { initTRPC, TRPCError } from '@trpc/server'
import superjson from 'superjson'
import { auth } from '@clerk/nextjs/server'
import { db } from '@/db'
import { users } from '@/db/schema'
import { eq } from 'drizzle-orm'
import { ratelimit } from '@/lib/ratelimit'

export const createContext = async () => {
  const { userId } = await auth()

  return {
    clerkUserId: userId
  }
}
export type Context = Awaited<ReturnType<typeof createContext>>

const t = initTRPC.context<Context>().create({
  transformer: superjson
})

export const router = t.router
export const procedure = t.procedure
export const protectedProcedure = t.procedure.use(async opts => {
  if (!opts.ctx.clerkUserId) {
    throw new TRPCError({ code: 'UNAUTHORIZED' })
  }
  const [user] = await db
    .select()
    .from(users)
    .where(eq(users.clerkId, opts.ctx.clerkUserId))
    .limit(1)

  if (!user) {
    throw new TRPCError({ code: 'UNAUTHORIZED' })
  }

  const { success } = await ratelimit.limit(user.id)
  if (!success) {
    throw new TRPCError({ code: 'TOO_MANY_REQUESTS' })
  }

  return opts.next({
    ctx: {
      ...opts.ctx,
      user
    }
  })
})
